#line 1 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
#line 1 "c:/users/toanrv/dropbox/pic project/source_and_design/digital clock/ds1307.h"
#line 24 "c:/users/toanrv/dropbox/pic project/source_and_design/digital clock/ds1307.h"
typedef struct{
 unsigned short sec;
 unsigned short min;
 unsigned short week_day;
 unsigned short day;
 unsigned short hour;
 unsigned short month;
 unsigned short year;
} RTC_Time;

void DS1307_Init();
unsigned short DS1307_BCD2Dec(unsigned char n);
unsigned short DS1307_Dec2BCD(unsigned char n) ;

void DS1307_GetDateTime(RTC_Time *p) ;

void DS1307_LCD_Display_Time(RTC_Time *p);

void DS1307_SetDateTime(RTC_Time *p);
#line 1 "c:/users/toanrv/dropbox/pic project/source_and_design/digital clock/rfremotedecode.h"
#line 24 "c:/users/toanrv/dropbox/pic project/source_and_design/digital clock/rfremotedecode.h"
 typedef enum RFRemoteDecode_State { RFRemoteDecode_Detect_Sync_Bit,
 RFRemoteDecode_Detect_a,
 RFRemoteDecode_Detect_FrameOrder,
 RFRemoteDecode_Detect_Encode,
 RFRemoteDecode_Process_Button,
 } ;
#line 648 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 unsigned short Tmr1_Cnt ;
 unsigned short Tmr0_Cnt ;
 unsigned int HighTickCounter ;
 unsigned int LowTickCounter ;
 unsigned short CntFrame ;
 unsigned short State ;
 unsigned short SyncBitCouter;

 bit Detect31aPulseFlag ;
 bit Detect1aPulseFlag ;
 bit DetectSyncBit ;
 bit EndOfTimingWindows ;

 char txt[8];
 char txt1[6];
#line 669 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 void interrupt(void){

 if(TMR0IF_bit){

 Tmr0_Cnt++;
 if(Tmr0_Cnt == 7){
 RC0_bit =~RC0_bit;
 Tmr0_Cnt = 0;
 EndOfTimingWindows = 1;
 }

 TMR0 = 0x00;
 TMR0IF_bit = 0;
 }
 if( PIR1.TMR1IF){

 Tmr1_Cnt++;
 RC3_bit=~ RC3_bit;
 TMR1H = 0x00;
 TMR1L = 0x00;
 PIR1.TMR1IF = 0;
 }
 if(INTF_bit){
 if(State == RFRemoteDecode_Detect_Sync_Bit){
 if(RB0_bit == 0){
#line 698 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 HighTickCounter = (unsigned int )(TMR1L | (TMR1H << 8));
 TMR1L = 0;
 TMR1H = 0;
#line 703 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 if( (HighTickCounter > 0 )&&(HighTickCounter < 2048) &&(Tmr1_Cnt==0) ){
#line 708 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 Detect1aPulseFlag =1 ;
 }
 else{
 Detect1aPulseFlag = 0;
 Tmr1_Cnt =0;

 }
 INTEDG_bit = 1;
 }

 else {
#line 723 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 LowTickCounter = (TMR1L | (TMR1H << 8)) ;
 TMR1L = 0;
 TMR1H = 0;

 if((LowTickCounter >= ((HighTickCounter << 5)-(HighTickCounter << 2) )) &&
 (LowTickCounter <= (HighTickCounter << 5)+ (HighTickCounter << 3)) &&
 (Detect1aPulseFlag) && (Tmr1_Cnt ==0) && (LowTickCounter!=0)){
#line 734 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 DetectSyncBit = 1;
 SyncBitCouter++;
 RC2_bit = ~ RC2_bit;
 }
 else {
 LowTickCounter = 0;
 HighTickCounter = 0;

 }

 Detect1aPulseFlag = 0;
 Tmr1_Cnt = 0;
 INTEDG_bit = 0;
 }
 }
 INTF_bit = 0;
 }
 }
#line 755 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 void main(void){

 UART1_Init(19200);
 Delay_ms(100);
 UART1_Write_Text("Start");

 Tmr1_Cnt = 0;
 Tmr0_Cnt = 0;
 HighTickCounter = 0;
 LowTickCounter = 0;
 CntFrame = 0;
 SyncBitCouter = 0;
 State = RFRemoteDecode_Detect_Sync_Bit;


 Detect31aPulseFlag = 0 ;
 Detect1aPulseFlag = 0 ;
 DetectSyncBit = 0 ;
 EndOfTimingWindows = 0 ;
#line 778 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 TRISC = 0b00000000;
 PORTC = 0x00;
#line 820 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 TRISB =0x00000001;
 OPTION_REG =0b01000000 ;


 INTE_bit = 0;
 INTF_bit = 0;
#line 872 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 PS2_bit = 1;
 PS1_bit = 1;
 PS0_bit = 1;
 TMR0 = 0x00;
 TMR0IE_bit = 0;
 TMR0IF_bit = 0;
#line 881 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 INTCON = 0b11000000;
 T1CON = 1;
 PIR1.TMR1IF = 0;
 PIE1.TMR1IE = 1;
 T1CKPS0_bit = 0;
 T1CkPS1_bit = 0;

 TMR1H =0x00;
 TMR1L =0x00;
#line 896 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 TMR0IE_bit = 1;
 INTE_bit = 1;
 INTEDG_bit = 1;

 while(1){

 if(DetectSyncBit){
 if((EndOfTimingWindows) ){
#line 909 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 if((SyncBitCouter >= 5) && (HighTickCounter > 0) &&(HighTickCounter < 2048) ){
 RC1_bit = ~RC1_bit;


 WordToStr(HighTickCounter, txt1);
#line 918 "C:/Users/Toanrv/Dropbox/PIC project/Source_and_Design/Digital clock/SmartDigitalClock.c"
 UART1_Write_Text("HighTickCouter:");
 UART1_Write_Text(txt1);
 UART1_Write_Text("\r\n");



 }


 SyncBitCouter = 0;
 EndOfTimingWindows = 0;

 }
 DetectSyncBit = 0;
 }

 }
 }
